package cn.aaa.service.impl;

import cn.aaa.openFeign.UaaOpenFeign;
import cn.aaa.pojo.domain.Driver;
import cn.aaa.mapper.DriverMapper;
import cn.aaa.pojo.domain.DriverSummary;
import cn.aaa.pojo.domain.DriverWallet;
import cn.aaa.pojo.domain.LoginVo;
import cn.aaa.result.JSONResult;
import cn.aaa.service.IDriverService;
import cn.aaa.service.IDriverSummaryMapper;
import cn.aaa.service.IDriverWalletMapper;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author axy
 * @since 2025-03-26
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private IDriverWalletMapper driverWalletMapper;
    @Autowired
    private IDriverSummaryMapper driverSummaryMapper;
    @Autowired
    private UaaOpenFeign uaaOpenFeign;
    /*司机注册*/
    @Transactional
    @Override
    public JSONResult register(Driver driver) {
        //根据openId去数据库里查询 如果有则登录,没有则保存
        Driver driver2 =
                driverMapper.selectOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, driver.getOpenId()));
        if (driver2 != null) {
            return JSONResult.success();
        }else {
            //初始化司机信息
            Date date = new Date();
            Driver driver1 = new Driver();
            driver1.setOpenId(driver.getOpenId());
            driver1.setPhone(driver.getPhone());
            driver1.setCreateTime(date);
            driver1.setUpdateTime(date);
            long id = IdUtil.getSnowflake(1, 1).nextId();
            driver1.setId(id);
            super.save(driver1);
            //初始化钱包
            DriverWallet driverWallet = new DriverWallet();
            driverWallet.setId(id);
            driverWallet.setAmount(new BigDecimal(0));
            driverWallet.setCreateTime(date);
            driverWallet.setUpdateTime(date);
            String salt = UUID.randomUUID().toString();
            String password = SecureUtil.md5(salt+"123456");
            driverWallet.setPassword(password);
            driverWallet.setTotalRecharge(new BigDecimal(0));
            driverWallet.setTotalWithdraw(new BigDecimal(0));
            driverWalletMapper.save(driverWallet);
            //初始化结算信息
            DriverSummary driverSummary = new DriverSummary();
            driverSummary.setId(id);
            driverSummary.setCreateTime(date);
            driverSummary.setUpdateTime(date);
            driverSummaryMapper.save(driverSummary);
            //远程调用uaa
            LoginVo loginVo = new LoginVo();
            loginVo.setOpenId(driver.getOpenId());
            loginVo.setPhone(driver.getPhone());
            loginVo.setId(id);
            uaaOpenFeign.register(loginVo);
        }
        return null;
    }
}
